<?php
// No direct access
defined ('_JEXEC') or die();

jimport ('joomla.application.component.modellist');

class ShipnhanhModelUserorders extends JModelList
{
	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array('id', 'product_name', 'userid', 'date');
		}
		parent::__construct($config);
	}

	function getListQuery()
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->select('o.id, o.product_name, o.link, o.photo_link, o.color, o.size,
				o.quantity, o.web_price, o.phone, u.name AS username, u.email, o.date, o.note');
		$query->from('#__sn_user_orders AS o');
		$query->join('INNER', '#__users AS u ON (u.id = o.userid)');

		// Filter search
		$search = $this->getState('filter.search');

		if (!empty($search))
		{
			$where = '';
				
			if (is_numeric($search))
			{
				$where .= 'o.id=' . $db->getEscaped($search) . ' OR';
			}
			$where .= ' o.product_name LIKE ' . $db->quote('%' . $search . '%') . ' OR u.name LIKE ' . $db->quote('%' . $search . '%');
				
			$query->where($where);
		}

		$query->order($this->getState('list.ordering', 'o.id') .
				' ' . $this->getState('list.direction', 'ASC'));

		return $query;
	}

	protected function populateState($ordering = null, $direction = null)
	{
		// Load the filter state
		$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
		$this->setState('filter.search', $search);

		parent::populateState($ordering, $direction);
	}
}